const handler = {
    loadMonthDay,
};
let resData = {
    handle: '',
    data: '',
    ok: false,
    end() {
        postMessage(JSON.stringify({
            handle: this.handle,
            data: this.data,
            ok: this.ok,
        }));
    },
};
onmessage = function (e) {
    const handle = e.data;
    if (handler[handle]) {
        resData.handle = handle;
        handler[handle]();
    }
    else {
        postMessage('没有该方法');
    }
};

function getTodayDate() {
    const today = new Date();
    const year = today.getFullYear();
    const month = today.getMonth() + 1;
    const day = today.getDate();
    const week = today.getDay();
    getTodayDate = function () {
        return {
            year,
            month,
            day,
            week,
        };
    };
    return {
        year,
        month,
        day,
        week,
    };
}

function getDayByMonth(month) {
    const today = getTodayDate();
    switch (month) {
        case 4:
        case 6:
        case 9:
        case 11:
            return createDayList(30);
        case 2:
            let num = today.year % 4 === 0 ? 29 : 28;
            return createDayList(num);
        default:
            return createDayList(31);
    }
}

function createDayList(dayNum) {
    const dayList = [];
    for (let i = 1; i <= dayNum; i++) {
        dayList.push(i);
    }
    return dayList;
}

function getDayWeek(year, month, day) {
    const date = new Date(`${year}/${month}/${day}`);
    return date.getDay();
}

function loadMonthDay() {
    const today = getTodayDate();
    let monthList = [0, 0, 0, 0, 0, 0];
    let index = -1;
    monthList = monthList.map((month, i) => {
        month = today.month + i;
        if (month > 12) {
            month -= 12;
            today.year++;
        }
        return {
            year: today.year,
            month,
            week: getDayWeek(today.year, month, 1),
            dayList: (getDayByMonth(month)).map(day => {
                index++;
                const outmoded = equalByDate(today.year, month, day);
                return {
                    year: today.year,
                    month,
                    day,
                    index,
                    selected: false,
                    ban: outmoded < 0,
                    today: outmoded === 0,
                };
            }),
        };
    });
    resData.data = monthList;
    resData.ok = true;
    resData.end();
}

function equalByDate(y, m, d) {
    const today = getTodayDate();
    return new Date(`${y}-${m}-${d}`) - new Date(`${today.year}-${today.month}-${today.day}`);
}